package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.Operation;
import com.his.service.OperationService;
import com.his.utils.MyUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Operation)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:13:47
 */
@RestController
@RequestMapping("operation")
public class OperationController {
    /**
     * 服务对象
     */
    @Resource
    private OperationService operationService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Rsp selectOne(Integer id) {
        return this.operationService.queryById(id);

    }

    /**
     * 增加手术
     */
    @PostMapping("insertOne")
    @RequireRoles(roles = {"doctor"}, index = 0)
    public Rsp insertOne(@RequestHeader(Const.TOKEN) String token, @RequestBody Operation operation) {
        try {
            operation.setCreateId(MyUtil.getUserByToken(token));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this.operationService.insert(operation);
    }

    /*
     *删除手术
     */
    @DeleteMapping("deleteOne")
    public Rsp deleteByIdLogic(@RequestBody Operation Operation) {
        return operationService.deleteByIdLogic(Operation.getId());
    }

    /**
     * 修改手术
     */
    @PutMapping("updateOne")
    public Rsp updateOne(@RequestBody Operation Operation) {
        return operationService.update(Operation);
    }

    /**
     * 查询手术列表
     */
    @RequireRoles(roles = {"doctor", "admin"}, index = 0)
    @PostMapping("searchSomeOperationPage")
    public Rsp searchSomeOperationPage(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<Operation> pageParam) throws Exception {
        return Rsp.success(operationService.searchSomeOperationPage(pageParam, MyUtil.getUser(token)));
    }

    /**
     * 填写手术结果
     */
    @RequireRoles(roles = {"doctor"}, index = 0)
    @PostMapping("endOne")
    public Rsp endOne(@RequestHeader(Const.TOKEN) String token, @RequestBody Operation operation) {
        return operationService.endOne(token, operation);
    }
}
